package com.jxaummd.mushroom.main.index.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.JsonReader;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.JSONToken;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.google.gson.JsonObject;
import com.jxaummd.mushroom.R;
import com.jxaummd.mushroom.R2;
import com.jxaummd.mushroom.core.base.fragment.BaseBottomFragment;
import com.jxaummd.mushroom.core.util.net.RestClient;
import com.jxaummd.mushroom.main.data.beam.DataBean;
import com.jxaummd.mushroom.main.data.data.CurentData;
import com.jxaummd.mushroom.main.index.contract.IndexContract;
import com.jxaummd.mushroom.main.index.data.ControlModel;
import com.jxaummd.mushroom.main.index.presenter.IndexPresenter;

import butterknife.BindView;
import butterknife.OnClick;
import me.yokeyword.fragmentation.ISupportFragment;

public class IndexFragment extends BaseBottomFragment<IndexContract.Presenter>
        implements IndexContract.View {
    @BindView(R2.id.index_gif_fan)
    ImageView mIvGifFan = null;

    @BindView(R2.id.index_gif_hot_fan)
    ImageView mIvGifHotFan = null;

    @BindView(R2.id.index_gif_water)
    ImageView mIvGifWater = null;

    @BindView(R2.id.index_gif_mushroom)
    ImageView mTvGifMushroom = null;

    @BindView(R2.id.iv_index_btn_fan)
    ImageView mTvBtnFan = null;

    @BindView(R2.id.iv_index_btn_hot_fan)
    ImageView mTvBtnHotFan = null;

    @BindView(R2.id.iv_index_btn_cold_fan)
    ImageView mTvBtnColdFan = null;

    @BindView(R2.id.iv_index_btn_cold_hum)
    ImageView mTvBtnHum = null;

    @BindView(R2.id.tv_index_hit_text)
    TextView mDataHintText = null;

    @BindView(R2.id.s_index_model)
    Switch switchbutton = null;

    boolean control[] = {false, false, false, false};

    /**
     * 加湿器
     */
    @OnClick(R2.id.iv_index_btn_cold_hum)
    public void onClickHum() {
        if (control[0]) {
            mTvBtnHum.setImageResource(R.mipmap.gif_index_button);
        } else {
            Glide.with(this).load(R.mipmap.gif_index_button)
                    .into(mTvBtnHum);
        }
        control[0] = !control[0];
        updateState();
    }

    /***
     * 通风风扇
     */
    @OnClick(R2.id.iv_index_btn_fan)
    public void onClickFan() {
        if (control[1]) {
            mTvBtnFan.setImageResource(R.mipmap.gif_index_button);
        } else {
            Glide.with(this).load(R.mipmap.gif_index_button)
                    .into(mTvBtnFan);
        }
        control[1] = !control[1];
        updateState();

    }

    /**
     * 热风扇
     */
    @OnClick(R2.id.iv_index_btn_hot_fan)
    public void onClickHotfan() {
        if (control[2]) {
            mTvBtnHotFan.setImageResource(R.mipmap.gif_index_button);
        } else {
            Glide.with(this).load(R.mipmap.gif_index_button)
                    .into(mTvBtnHotFan);
        }
        control[2] = !control[2];
        updateState();

    }


    /**
     * 加湿器
     */
    @OnClick(R2.id.iv_index_btn_cold_fan)
    public void onClickColdFan() {
        if (control[3]) {
            mTvBtnColdFan.setImageResource(R.mipmap.gif_index_button);
        } else {
            Glide.with(this).load(R.mipmap.gif_index_button)
                    .into(mTvBtnColdFan);
        }
        control[3] = !control[3];
        updateState();
    }


    private void updateState() {

        if (control[0]) {
            Glide.with(this).load(R.mipmap.gif_index_water)
                    .into(mIvGifWater);
        } else {
            mIvGifWater.setImageResource(R.mipmap.gif_index_water);
        }


        if (control[1]) {
            Glide.with(this).load(R.mipmap.gif_index_fan)
                    .into(mIvGifFan);
        } else {
            mIvGifFan.setImageResource(R.mipmap.gif_index_fan);
        }


        if (control[2]) {
            Glide.with(this).load(R.mipmap.git_hot_fan)
                    .into(mIvGifHotFan);
        } else {
            mIvGifHotFan.setImageResource(R.mipmap.git_hot_fan);
        }


        if (control[3]) {

            Glide.with(this).load(R.mipmap.git_hot_fan)
                    .into(mIvGifHotFan);
        } else {
            mIvGifHotFan.setImageResource(R.mipmap.git_hot_fan);
        }

        if (control[0] || control[1] || control[3] || control[2]) {
            Glide.with(this).load(R.mipmap.gif_mushroom_max)
                    .into(mTvGifMushroom);
        } else {
            mTvGifMushroom.setImageResource(R.mipmap.gif_mushroom_max);
        }


        RestClient.builder()
                .url("http://47.107.183.2/mushroom/client/control?mbid=1")
                .params("enfan", control[1] ? 1 : 0)
                .params("enhotfan", control[2] ? 1 : 0)
                .params("encoldfan", control[3] ? 1 : 0)
                .params("enhum", control[0] ? 1 : 0)
                .success(response -> {
                    ToastUtils.showShort("状态切换成功");
                })
                .build()
                .get();


    }

    public static IndexFragment newInstance() {
        Bundle args = new Bundle();
        IndexFragment fragment = new IndexFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public Object setLayout() {
        return R.layout.index_layout;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mPresenter.start();
        syncData(1);
        //校准
        RestClient.builder()
                .url("http://47.107.183.2/mushroom/client/getcontrol")
                .params("mbid", 1)
                .success(response -> {
                    ControlModel model = JSON.parseObject(response, ControlModel.class);
                    control[0] = model.getMb_enhum() == 0 ? false : true;
                    control[1] = model.getMb_enfan() == 0 ? false : true;
                    control[2] = model.getMb_enhotfan() == 0 ? false : true;
                    control[3] = model.getMb_encoldfan() == 0 ? false : true;
                    onClickColdFan();
                    onClickFan();
                    onClickHotfan();
                    onClickHum();
                    updateState();
                })
                .build()
                .get();

     switchbutton.setOnCheckedChangeListener((buttonView, isChecked) -> {
         if(isChecked){
             ToastUtils.showShort("已经切换为自动模式");
//             control[0]=false;
//             control[1]=false;
//             control[2]=false;
//             control[3]=false;
//             onClickColdFan();
//             onClickFan();
//             onClickHotfan();
//             onClickHum();
//             updateState();
             RestClient.builder()
                     .url("http://47.107.183.2/mushroom/client/control?mbid=1")
                     .params("model",1)
                     .success(response -> {
                     })
                     .build()
                     .get();
         }else {
             ToastUtils.showShort("已经关闭自动模式");
             RestClient.builder()
                     .url("http://47.107.183.2/mushroom/client/control?mbid=1")
                     .params("model",0)
                     .success(response -> {
                     })
                     .build()
                     .get();
         }
     });
    }

    private void syncData(int i) {
        RestClient.builder()
                .url("http://47.107.183.2/mushroom/client/current/data")
                .params("mbid", i)
                .success(response -> {
                    CurentData data = JSON.parseObject(response, CurentData.class);
                    mDataHintText.setText("温度：" + data.getData().getMb_temp() + "\n湿度：" + data.getData().getMb_hum() + "\n成熟：" + "未成熟");
                    syncData(1);
                })
                .build()
                .get();

    }


    @Override
    public void onItemSelect(int position) {
    }

    @Override
    protected IndexContract.Presenter initPresenter() {
        return new IndexPresenter(this);
    }


    @Override
    public void startFragment(ISupportFragment fragment) {
        getMyParentFragment().start(fragment);
    }

}
